package cz.data.domain.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.system.mapstruct.QrtzJobLogMapper;
import cz.data.domain.system.model.dto.QrtzJobLogDto;
import cz.data.domain.system.model.entity.QrtzJobLogEntity;
import cz.data.domain.system.model.query.QrtzJobLogQuery;
import cz.data.domain.system.model.vo.QrtzJobLogVo;
import cz.data.domain.system.service.QrtzJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"定时任务日志"})
@RestController
public class QrtzJobLogController extends BaseController {

    @Autowired
    private QrtzJobLogService qrtzJobLogService;

    @Autowired
    private QrtzJobLogMapper qrtzJobLogMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务日志-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/quartz/logs/{id}")
    public SimpleDataResponse<QrtzJobLogVo> getQrtzJobLogById(@PathVariable String id) {
        QrtzJobLogEntity qrtzJobLogEntity = qrtzJobLogService.getQrtzJobLogById(id);
        return new SimpleDataResponse<>(qrtzJobLogMapper.toVO(qrtzJobLogEntity));
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务日志-分页查询")
    @ApiImplicitParam(name = "qrtzJobLogQuery", value = "查询实体qrtzJobLogQuery", required = true, dataTypeClass = QrtzJobLogQuery.class)
    @GetMapping("/quartz/logs/page")
    public SimpleDataResponse<JsonPage<QrtzJobLogVo>> getQrtzJobLogPage(QrtzJobLogQuery qrtzJobLogQuery) {
        QueryWrapper<QrtzJobLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(qrtzJobLogQuery.getJobId()), "job_id", qrtzJobLogQuery.getJobId());
        IPage<QrtzJobLogEntity> page = qrtzJobLogService.page(new Page<>(qrtzJobLogQuery.getPageNum(), qrtzJobLogQuery.getPageSize()), queryWrapper);
        List<QrtzJobLogVo> collect = page.getRecords().stream().map(qrtzJobLogMapper::toVO).collect(Collectors.toList());
        JsonPage<QrtzJobLogVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务日志-新增")
    @ApiImplicitParam(name = "qrtzJobLog", value = "详细实体qrtzJobLog", required = true, dataType = "QrtzJobLogDto")
    @PostMapping("/quartz/logs")
    public BaseResponse saveQrtzJobLog(@RequestBody @Validated({ValidationGroups.Insert.class}) QrtzJobLogDto qrtzJobLog) {
        qrtzJobLogService.saveQrtzJobLog(qrtzJobLog);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务日志-修改", notes = "根据url的id来指定修改对象，并根据传过来的信息来修改详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "qrtzJobLog", value = "详细实体qrtzJobLog", required = true, dataType = "QrtzJobLogDto")
    })
    @PutMapping("/quartz/logs/{id}")
    public BaseResponse updateQrtzJobLog(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) QrtzJobLogDto qrtzJobLog) {
        qrtzJobLogService.updateQrtzJobLog(qrtzJobLog);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "定时任务日志-删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/quartz/logs/{id}")
    public BaseResponse deleteQrtzJobLogById(@PathVariable String id) {
        qrtzJobLogService.deleteQrtzJobLogById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "定时任务日志-批量删除", notes = "根据url的ids来批量删除对象")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/quartz/logs/batch/{ids}")
    public BaseResponse deleteQrtzJobLogBatch(@PathVariable List<String> ids) {
        qrtzJobLogService.deleteQrtzJobLogBatch(ids);
        return BaseResponse.success();
    }
}
